package com.wtgroup.demo.leetcode.org_bytedance;

import java.util.LinkedList;

/**
 * 71. 简化路径 https://leetcode-cn.com/problems/simplify-path/
 *
 * @author dafei
 * @version 0.1
 * @date 2021/4/29 14:38
 */
public class Q_简化路径 {
    public static void main(String[] args) {
        // String input = "/a/./b/../../c/";
        String input = "/home//foo/";
        Q_简化路径 exe = new Q_简化路径();
        String s = exe.simplifyPath(input);
        System.out.println(s);
    }

    // "/a/./b/../../c/"
    public String simplifyPath(String path) {
        String[] split = path.split("/");
        LinkedList queue = new LinkedList();
        for (String s : split) {
            if (!s.isEmpty()) {
                if (s.equals(".")) {
                    continue;
                }
                if (s.equals("..")) {
                    if (!queue.isEmpty()) {
                        queue.removeLast();
                    }
                } else {
                    queue.addLast(s);
                }
            }
        }
        StringBuilder sb = new StringBuilder();
        for (Object o : queue) {
            if (sb.length() > 0) {
                sb.append("/");
            }
            sb.append(o);
        }
        return "/" + sb.toString();
    }



}
